#!/usr/bin/env python
# coding: utf-8
import psutil,hashlib,base64,rsa,os
from datetime import datetime,timedelta
data_type_dict = {'avg_no2':u'NO₂(μg/m³)',
                'avg_o3':u'O₃(μg/m³)',
                'avg_pm10':u'PM10(μg/m³)',
                'avg_pm25':u'PM2.5(μg/m³)',
                'avg_co':u'CO(mg/m³)',
                'avg_so2':u'SO₂(μg/m³)',
                'avg_humidity':u'湿度(%rh)',
                'avg_wind_direction':u'风向(a)',
                'avg_wind_speed':u'风速(m/s)',
                'avg_atmosphere':u'大气压(MPa)',
                'avg_temperature':u'温度(°C)'}
data_type_title_dict = {'avg_no2':u'NO₂',
                'avg_o3':u'O₃',
                'avg_pm10':u'PM10',
                'avg_pm25':u'PM2.5',
                'avg_co':u'CO',
                'avg_so2':u'SO₂',
                'avg_humidity':u'湿度',
                'avg_wind_direction':u'风向',
                'avg_wind_speed':u'风速',
                'avg_atmosphere':u'大气压',
                'avg_temperature':u'温度'}
#反转字典 即key和val互换
def invert_dict(d):
    return dict((v,k) for k,v in d.iteritems())

#二维数组转字符串
def two_array_to_utf_string(tmp_array):
    result=""
    line=""
    if tmp_array:
        for item in tmp_array:
            line=line+array_to_utf_string(item)+","

    result="["+line[:-1]+"]"
    return result

def array_to_utf_string(tmp_array):
    if tmp_array:
        return "['"+"','".join(tmp_array)+"']"
    else:
        return ""

def create_date_array(start_date,end_date):
    title_array=[]
    begin = datetime.strptime(start_date, "%Y-%m-%d")
    end = datetime.strptime(end_date, "%Y-%m-%d")
    for i in range((end - begin).days+1):
        day = begin + timedelta(days=i)
        title_array.append(day.strftime("%Y-%m-%d"))
    return title_array

def create_date_24hour_array(start_date,end_date):
    title_array=[]
    begin = datetime.strptime(start_date, "%Y-%m-%d")
    end = datetime.strptime(end_date, "%Y-%m-%d")
    for i in range((end - begin).days+1):
        day = begin + timedelta(days=i)
        #print str(day)
        for i in range(24):
            hour=u'%02d时' % i
            title_array.append(day.strftime("%Y-%m-%d")+" "+hour)
    return title_array

def create_hour_title_array(start_hour,end_hour):
    title_array=[]
    for i in range(24):
        hour='%02d' % i
        if hour>=start_hour and hour<=end_hour:
            title_array.append(hour)
    return title_array

def create_field_title_by_avg_name(data_types):
    title_array=[]
    if data_types:
        for data in data_types:
            title_array.append(data_type_dict[data])
    return title_array

#从data list中去日期列表，去重并排序
def get_date_sort_array_from_list(data_list):
    date_array=[]
    for data in data_list:
        date_array.append(str(data.collection_date))
    date_array=list(set(date_array))
    date_array.sort()
    return date_array

def get_month_sort_array_from_list(data_list):
    date_array=[]
    for data in data_list:
        date_array.append(str(data.collection_month))
    date_array=list(set(date_array))
    date_array.sort()
    return date_array

#在data_list中查找监测站名称、日期符合的记录，找到后返回指定数据列的值,没找到返回''
def query_site_date_type_value(station,selected_date,data_type,data_list):
    result=''
    for data in data_list:
        if data.site==station and data.collection_date.strftime("%Y-%m-%d")==selected_date:
            result=str(data[data_type])
            break
    return result

def query_site_month_type_value(station,selected_date,data_type,data_list):
    result=''
    for data in data_list:
        if data.site==station and data.collection_month==selected_date:
            result=str(data[data_type])
            break
    return result

def is_station_in_data_list(station_name,data_list):
    station_exist=False
    for data in data_list:
        if data.site==station_name:
            #监测站是存在的
            station_exist=True
            break
    return station_exist

#第一列为时间，后面依次为各类型的数值
def date_first_type_next_array(first_col,data_types,data_list):
    result_array=[]
    for data in data_list:
        row_array=[]
        if first_col == "collection_month":
            row_array.append(data.collection_month)
        if first_col == "collection_date":
            row_array.append(str(data.collection_date))
        if first_col == "collection_hour":
            row_array.append(data.collection_hour)
        if 'avg_no2' in data_types:
            row_array.append(str(data.avg_no2))
        if 'avg_o3' in data_types:
            row_array.append(str(data.avg_o3))
        if 'avg_pm10' in data_types:
            row_array.append(str(data.avg_pm10))
        if 'avg_pm25' in data_types:
            row_array.append(str(data.avg_pm25))
        if 'avg_co' in data_types:
            row_array.append(str(data.avg_co))
        if 'avg_so2' in data_types:
            row_array.append(str(data.avg_so2))
        if 'avg_humidity' in data_types:
            row_array.append(str(data.avg_humidity))
        if 'avg_wind_direction' in data_types:
            row_array.append(str(data.avg_wind_direction))
        if 'avg_wind_speed' in data_types:
            row_array.append(str(data.avg_wind_speed))
        if 'avg_atmosphere' in data_types:
            row_array.append(str(data.avg_atmosphere))
        if 'avg_temperature' in data_types:
            row_array.append(str(data.avg_temperature))
        result_array.append(row_array)
    return result_array



#第一列为时段，后面依次为各类型的数值
def hour_range_first_type_next_array(first_col,data_types,data_list):
    result_array=[]
    for data in data_list:
        row_array=[]
        if first_col == "collection_month":
            row_array.append(data.collection_month)
        if first_col == "collection_date":
            row_array.append(str(data.collection_date))
        if first_col == "collection_hour":
            row_array.append(data.collection_hour)
        if 'avg_no2' in data_types:
            row_array.append(str(data.avg_no2))
        if 'avg_o3' in data_types:
            row_array.append(str(data.avg_o3))
        if 'avg_pm10' in data_types:
            row_array.append(str(data.avg_pm10))
        if 'avg_pm25' in data_types:
            row_array.append(str(data.avg_pm25))
        if 'avg_co' in data_types:
            row_array.append(str(data.avg_co))
        if 'avg_so2' in data_types:
            row_array.append(str(data.avg_so2))
        if 'avg_humidity' in data_types:
            row_array.append(str(data.avg_humidity))
        if 'avg_wind_direction' in data_types:
            row_array.append(str(data.avg_wind_direction))
        if 'avg_wind_speed' in data_types:
            row_array.append(str(data.avg_wind_speed))
        if 'avg_atmosphere' in data_types:
            row_array.append(str(data.avg_atmosphere))
        if 'avg_temperature' in data_types:
            row_array.append(str(data.avg_temperature))
        result_array.append(row_array)
    return result_array

#第一列为数据类型，后面依次为各时间的数值
def type_first_date_next_array(data_type_name,data_list):
    result_array=[]
    row_array=[]
    row_array.append(data_type_dict[data_type_name])
    for data in data_list:
        if 'avg_no2' == data_type_name:
            row_array.append(str(data.avg_no2))
        if 'avg_o3' == data_type_name:
            row_array.append(str(data.avg_o3))
        if 'avg_pm10' == data_type_name:
            row_array.append(str(data.avg_pm10))
        if 'avg_pm25' == data_type_name:
            row_array.append(str(data.avg_pm25))
        if 'avg_co' == data_type_name:
            row_array.append(str(data.avg_co))
        if 'avg_so2' == data_type_name:
            row_array.append(str(data.avg_so2))
        if 'avg_humidity' == data_type_name:
            row_array.append(str(data.avg_humidity))
        if 'avg_wind_direction' == data_type_name:
            row_array.append(str(data.avg_wind_direction))
        if 'avg_wind_speed' == data_type_name:
            row_array.append(str(data.avg_wind_speed))
        if 'avg_atmosphere' == data_type_name:
            row_array.append(str(data.avg_atmosphere))
        if 'avg_temperature' == data_type_name:
            row_array.append(str(data.avg_temperature))

    return row_array


